// 前端API配置文件
const API_CONFIG = {
  // 充值服务配置 (8084端口)
  RECHARGE_BASE_URL: 'http://localhost:8084',
  
  // 患者管理服务配置 (8080端口)
  PATIENT_BASE_URL: 'http://localhost:8080',
  
  // API端点配置
  ENDPOINTS: {
    // 充值记录相关
    RECHARGE_LIST: '/recharge/list',
    RECHARGE_DETAIL: '/recharge/detail',
    RECHARGE_ADD: '/recharge/add',
    RECHARGE_UPDATE: '/recharge/update',
    RECHARGE_DELETE: '/recharge/delete',
    RECHARGE_HEALTH: '/recharge/health',
    
    // 患者相关
    PATIENT_INFO: '/patient/info',
    PATIENT_LIST: '/patient/list'
  }
}

// 充值记录API函数
const rechargeAPI = {
  // 获取充值记录列表
  getList: (params = {}) => {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${API_CONFIG.RECHARGE_BASE_URL}${API_CONFIG.ENDPOINTS.RECHARGE_LIST}`,
        method: 'GET',
        data: params,
        header: {
          'Content-Type': 'application/json',
          'Authorization': wx.getStorageSync('token') || ''
        },
        success: (res) => {
          if (res.statusCode === 200) {
            resolve(res.data)
          } else {
            reject(new Error(`请求失败: ${res.statusCode}`))
          }
        },
        fail: (error) => {
          console.error('网络请求失败:', error)
          reject(new Error('网络请求失败'))
        }
      })
    })
  },

  // 获取充值记录详情
  getDetail: (orderId) => {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${API_CONFIG.RECHARGE_BASE_URL}${API_CONFIG.ENDPOINTS.RECHARGE_DETAIL}/${orderId}`,
        method: 'GET',
        header: {
          'Content-Type': 'application/json',
          'Authorization': wx.getStorageSync('token') || ''
        },
        success: (res) => {
          if (res.statusCode === 200) {
            resolve(res.data)
          } else {
            reject(new Error(`请求失败: ${res.statusCode}`))
          }
        },
        fail: (error) => {
          console.error('网络请求失败:', error)
          reject(new Error('网络请求失败'))
        }
      })
    })
  },

  // 健康检查
  health: () => {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${API_CONFIG.RECHARGE_BASE_URL}${API_CONFIG.ENDPOINTS.RECHARGE_HEALTH}`,
        method: 'GET',
        success: (res) => {
          if (res.statusCode === 200) {
            resolve(res.data)
          } else {
            reject(new Error(`健康检查失败: ${res.statusCode}`))
          }
        },
        fail: (error) => {
          console.error('健康检查失败:', error)
          reject(new Error('健康检查失败'))
        }
      })
    })
  }
}

module.exports = {
  API_CONFIG,
  rechargeAPI
} 